package com.hey.snmp.server.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import com.hey.mib.entity.BaseInterface;
import com.hey.mib.proxy.util.ProxyUtil;
import com.hey.snmp.server.SNMPAPI;
import com.hey.snmp.utils.*;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.event.ResponseListener;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;

public class SNMPAPIImpl implements SNMPAPI {

	@Override
	public void checkSnmpAgentActive(SNMPTarget target) throws Exception {

		//1：初始化SNMP
		Snmp snmp = SnmpUtil.initSnmp(target);
		//2：创建target
		Target target1 = SnmpUtil.createTarget(target, DefaultConstant.SNMP_COMMUNITY_GET);
		//3：创建PDU
		PDU pdu = new PDU();
		//4:连接
		ResponseEvent respEvent = snmp.send(pdu, target1);
		//关闭
		SnmpUtil.closeSnmp(snmp);
	}

	@Override
	public String getOIDValue(SNMPTarget target, String paramOid) throws Exception {
		String result = "";
		//1：初始化SNMP
		Snmp snmp = SnmpUtil.initSnmp(target);
		//2：创建target
		Target target1 = SnmpUtil.createTarget(target, DefaultConstant.SNMP_COMMUNITY_GET);
		//3：创建PDU
		PDU pdu = SnmpUtil.createPdu(target.snmpVersion,PDU.GET,paramOid,"");
		//4:发送报文
		ResponseEvent respEvent = snmp.send(pdu, target1);
		//5：得到结果
		PDU response = respEvent.getResponse();
		if (response == null) {
			System.out.println("response is null, request time out");
		}else {
			for (int i = 0; i < response.size(); i++) {
				VariableBinding vb = response.get(i);
				result = vb.getVariable().toString();
//		          System.out.println(vb.getOid() + " = " + vb.getVariable());
			}
		}
		//关闭
		SnmpUtil.closeSnmp(snmp);
		return result;
	}

	@Override
	public long getOIDLongValue(SNMPTarget target, String paramOid) throws Exception {
		Long result = 0L;
		//1：初始化SNMP
		Snmp snmp = SnmpUtil.initSnmp(target);
		//2：创建target
		Target target1 = SnmpUtil.createTarget(target, DefaultConstant.SNMP_COMMUNITY_GET);
		//3：创建PDU
		PDU pdu = SnmpUtil.createPdu(target.snmpVersion,PDU.GET,paramOid,"");
		//4:发送报文
		ResponseEvent respEvent = snmp.send(pdu, target1);
		//5：得到结果
		PDU response = respEvent.getResponse();
		if (response == null) {
			System.out.println("response is null, request time out");
		}else {
			for (int i = 0; i < response.size(); i++) {
				VariableBinding vb = response.get(i);
				result = NumberUtil.str2Long(vb.getVariable().toString());
			}
		}
		//关闭
		SnmpUtil.closeSnmp(snmp);
		return result;
	}

	@Override
	public String getNextOIDValue(SNMPTarget target, String paramOid) throws Exception {
		String result = "";
		//1：初始化SNMP
		Snmp snmp = SnmpUtil.initSnmp(target);
		//2：创建target
		Target target1 = SnmpUtil.createTarget(target, DefaultConstant.SNMP_COMMUNITY_GET);
		//3：创建PDU
		PDU pdu = SnmpUtil.createPdu(target.snmpVersion,PDU.GET,paramOid,"");
		//4:发送报文
		ResponseEvent respEvent = snmp.getNext(pdu, target1);
		//5：得到结果
		PDU response = respEvent.getResponse();
		if (response == null) {
			System.out.println("response is null, request time out");
		}else {
			for (int i = 0; i < response.size(); i++) {
				VariableBinding vb = response.get(i);
				result = vb.getVariable().toString();
//		          System.out.println(vb.getOid() + " = " + vb.getVariable());
			}
		}
		//关闭
		SnmpUtil.closeSnmp(snmp);
		return result;
	}

	@Override
	public Object getObject(SNMPTarget target, BaseInterface paramObj,Boolean syn) throws Exception {
		//1：初始化SNMP
		Snmp snmp = SnmpUtil.initSnmp(target);
		//2：创建target
		Target target1 = SnmpUtil.createTarget(target, DefaultConstant.SNMP_COMMUNITY_GET);
		//2:得到OID
		ProxyUtil proxyUtil = new ProxyUtil(paramObj);
		//索引
		String table_index = proxyUtil.getIndexValue();
		//表OID
		String oid = proxyUtil.getOIDMapping()+".oid." + table_index;
		//参数OID集合
		Map map = proxyUtil.getIndexMap();
		List<String> param_oid_list = new ArrayList<>();
		for (Object param_index: map.values()) {
			param_oid_list.add(oid.replaceAll("oid",param_index.toString()));
		}
		//3：创建PDU
		PDU pdu = SnmpUtil.createPdus(target.snmpVersion,PDU.GET,param_oid_list,null);
		//4:发送报文
		List list = new ArrayList();
		PDU response;
		if(syn){
			//同步
			ResponseEvent respEvent = snmp.send(pdu, target1);
			response = respEvent.getResponse();
			if (response == null) {
				System.out.println("response is null, request time out");
			}else {
				for (int i = 0; i < response.size(); i++) {
					VariableBinding vb = response.get(i);
					list.add(vb.getVariable());
					System.out.println(vb.getOid() + " = " + vb.getVariable());
				}
			}
		}else {
			//异步
			// 设置监听对象
			ResponseListener listener = new SnmpResponse();
			//list = ((SnmpResponse) listener).getList();
			// 发送报文
			snmp.send(pdu, target1, null, listener);
		}
		//6:List转Object
		paramObj = proxyUtil.setValue(list);
		//关闭
		SnmpUtil.closeSnmp(snmp);
		return paramObj;
	}

	@Override
	public List getAllTableData(SNMPTarget target, BaseInterface paramClass) throws Exception {
		List list = new ArrayList<>();
		//1：初始化SNMP
		Snmp snmp = SnmpUtil.initSnmp(target);
		//2：创建target
		Target target1 = SnmpUtil.createTarget(target, DefaultConstant.SNMP_COMMUNITY_GET);
		//2:得到OID
		ProxyUtil proxyUtil = new ProxyUtil(paramClass);
		String table_oid = proxyUtil.getOIDMapping();
		//3：创建PDU
		PDU pdu = SnmpUtil.createPdu(target.snmpVersion,PDU.GETNEXT,table_oid,"");
		//取值
		List val_list = SnmpUtil.snmpTableWalk(snmp,target1,pdu,table_oid);
		//解析值
		Map map = proxyUtil.getIndexMap();
		int param_size = map.size();
		if (val_list != null && val_list.size() > 0 && val_list.size()%param_size == 0){
			int length = val_list.size()/param_size;
			for(int i = 0;i<length;i++){
				List obj_list = new ArrayList();
				for(int j=0;j<param_size;j++){
					obj_list.add(val_list.get(length*j+i));
				}
				//6:List转Object
				paramClass =  proxyUtil.setValue(obj_list);
				list.add(paramClass);
			}
		}
		//关闭
		SnmpUtil.closeSnmp(snmp);
		return list;
	}

	@Override
	public void setOIDValue(SNMPTarget target, String goalOid, String goalIndex, Object goalVal) throws Exception {
		//1：初始化SNMP
		Snmp snmp = SnmpUtil.initSnmp(target);
		//2：创建target
		Target target1 = SnmpUtil.createTarget(target, DefaultConstant.SNMP_COMMUNITY_SET);
		//3：创建PDU
		PDU pdu = SnmpUtil.createPdu(target.snmpVersion,PDU.SET,goalOid + "." + goalIndex,goalVal);
		//4:发送报文
		ResponseEvent respEvent = snmp.send(pdu, target1);
		//5：得到结果
		PDU response = respEvent.getResponse();
		//关闭
		SnmpUtil.closeSnmp(snmp);
	}

	@Override
	public void updateObject(SNMPTarget target, BaseInterface paramObj) throws Exception {
		//1：初始化SNMP
		Snmp snmp = SnmpUtil.initSnmp(target);
		//2：创建target
		Target target1 = SnmpUtil.createTarget(target, DefaultConstant.SNMP_COMMUNITY_SET);
		//2.0:得到索引
		ProxyUtil proxyUtil = new ProxyUtil(paramObj);
		//索引
		String table_index = proxyUtil.getIndexValue();
		//表OID
		String oid = proxyUtil.getOIDMapping()+".oid." + table_index;
		//参数OID集合
		Map map = proxyUtil.getIndexMap();
		List<String> param_oid_list = new ArrayList<>();
		for (Object param_index: map.values()) {
			param_oid_list.add(oid.replaceAll("oid",param_index.toString()));
		}
		param_oid_list.remove(0);
		//得到值的集合
		Map valsMap = proxyUtil.getValues();
		List param_val_list = new ArrayList(valsMap.values());
		param_val_list.remove(0);
		//3：创建PDU
		PDU pdu = SnmpUtil.createPdus(target.snmpVersion,PDU.SET,param_oid_list,param_val_list);
		//4:发送报文
		ResponseEvent respEvent = snmp.send(pdu, target1);
		//5：得到结果
		PDU response = respEvent.getResponse();
		//关闭
		SnmpUtil.closeSnmp(snmp);
	}

	@Override
	public void updateParam(SNMPTarget target, BaseInterface paramObj, List<String> paramList) throws Exception {
		//1：初始化SNMP
		Snmp snmp = SnmpUtil.initSnmp(target);
		//2：创建target
		Target target1 = SnmpUtil.createTarget(target, DefaultConstant.SNMP_COMMUNITY_SET);
		//得到索引
		ProxyUtil proxyUtil = new ProxyUtil(paramObj);
		//索引
		String table_index = proxyUtil.getIndexValue();
		//表OID
		String oid = proxyUtil.getOIDMapping()+".oid." + table_index;
		//参数OID集合
		Map oidsMap = proxyUtil.getIndexMap();
		//得到值的集合
		Map valsMap = proxyUtil.getValues();
		List<String> param_oid_list = new ArrayList<>();
		List param_val_list = new ArrayList();
		if (paramList != null && paramList.size() > 0){
			for (String param: paramList) {
				if (oidsMap.get(param) != null){
					param_oid_list.add(oid.replaceAll("oid",oidsMap.get(param).toString()));
				}
				if (valsMap.get(param) != null){
					param_val_list.add(valsMap.get(param));
				}
			}
		}
		//3：创建PDU
		PDU pdu = SnmpUtil.createPdus(target.snmpVersion,PDU.SET,param_oid_list,param_val_list);
		//4:发送报文
		ResponseEvent respEvent = snmp.send(pdu, target1);
		//5：得到结果
		PDU response = respEvent.getResponse();
		//关闭
		SnmpUtil.closeSnmp(snmp);

	}

	@Override
	public void addTableRow(SNMPTarget target, BaseInterface paramObj) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void delTableRow(SNMPTarget target, BaseInterface paramObj) throws Exception {
		// TODO Auto-generated method stub

	}

}
